﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GoShopping.Web.UserControls
{
    public partial class advisory : System.Web.UI.UserControl
    {
        BLL.SysInfo.AdvisoryBean ab = new GoShopping.BLL.SysInfo.AdvisoryBean();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetAdvisory();
                SetAdvisoryTitle();
            }
        }

        private void GetAdvisory()
        {
            ListView1.DataSource = ab.GetAllAdvisory(5);
            ListView1.DataBind();
        }
        void SetAdvisoryTitle()
        {
            IList<Utility.EnumBoundings.ComboBoxItem> adiList = Utility.EnumBoundings.OptionEnum.GetEnumNameValueKeys(Model.Enumerations.AdvisoryType.GoodsAdvisory.GetType());
            ad_topic.DataTextField = "Name";
            ad_topic.DataValueField = "Value";
            ad_topic.DataSource = adiList;
            ad_topic.DataBind();
        }

        protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (ListViewItemType.DataItem == e.Item.ItemType)
            {
                Label lblReply = e.Item.FindControl("lblReply") as Label;
                if (lblReply.Text != "")
                {
                    lblReply.Text = "回复:" + lblReply.Text;
                }
            }
        }
        protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            SubmitAdvisory();
        }
        protected void but_annoy_Click(object sender, EventArgs e)
        {
            SubmitAdvisory();
        }
        protected void but_real_Click(object sender, EventArgs e)
        {
            if (Context.User.Identity.IsAuthenticated)
            {
                SubmitAdvisory();
            }
            else
            {
                Response.Redirect("~/Login.aspx");
            }
        }

        private void SubmitAdvisory()
        {
            if (string.IsNullOrEmpty(txtContent.Text.Trim()))
            {
                return;
            }
            Model.Advisory adv = new GoShopping.Model.Advisory();
            if (Context.User.Identity.IsAuthenticated)
            {
                adv.UserName = Context.User.Identity.Name;
            }
            else
            {
                adv.UserName = "annoy";
            }
            adv.Title = ad_topic.SelectedItem.Text.ToString();
            adv.Content = txtContent.Text.Trim();
            adv.CreateDate = System.DateTime.Now;
            adv.IsReply = false;
            ab.AddOneAdvisory(adv);
            GetAdvisory();
        }
    }
}